package hongcaosp.app.android.contact.fans;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.target.ImageViewTarget;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.common.PresenterListener;
import hongcaosp.app.android.modle.bean.IFans;
import hongcaosp.app.android.modle.bean.IUser;
import hongcaosp.app.android.user.UserDetailActivity;
import hongcaosp.app.android.video.player.VideoPresenter;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.GlideUtils;
import xlj.lib.android.base.view.CircleImageView;
import xlj.lib.android.base.view.OnRecycleItemClickListener;

public class FansHolder extends BaseHolder<IFans> {
    private Context context;
    private ImageView userLogoIV;
    private TextView userNameTV,timeTV,descripTV,followBTN;
    private VideoPresenter videoPresenter;

    public FansHolder(View itemView, final Context context) {
        super(itemView);
        this.context = context;
        userLogoIV = itemView.findViewById(R.id.user_logo);
        userNameTV = itemView.findViewById(R.id.user_name);
        timeTV = itemView.findViewById(R.id.fan_time);
        descripTV = itemView.findViewById(R.id.descrip);
        followBTN = itemView.findViewById(R.id.follow_btn);

        videoPresenter = new VideoPresenter();

        followBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getData() != null) {
                    IFans fans = getData();
                    if (fans.meFollowTa()) {
                        videoPresenter.cancelFollowUser(getData(), new PresenterListener() {
                            @Override
                            public void onSuccess() {
                                freshFollow();
                            }

                            @Override
                            public void onFail(String message) {
                                ToastManager.getInstance().showToast(message);
                            }
                        });
                    } else {
                        videoPresenter.followUser(getData(), new PresenterListener() {
                            @Override
                            public void onSuccess() {
                                freshFollow();
                            }

                            @Override
                            public void onFail(String message) {
                                ToastManager.getInstance().showToast(message);
                            }
                        });
                    }
                }
            }
        });

        descripTV.setText("关注了我");

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserDetailActivity.toDetail(context,getData().getUserId());
            }
        });
    }

    @Override
    public void setData(final IFans fans) {
        super.setData(fans);
        GlideUtils.loadCircleImage(context,fans.getUserLogo(),userLogoIV,R.drawable.icon_head);
        userNameTV.setText(fans.getUserName());
        timeTV.setText(fans.getTime());

        freshFollow();
    }

    public void freshFollow() {
        if (getData().meFollowTa()) {
            followBTN.setSelected(true);
            followBTN.setText("互相关注");
        } else {
            followBTN.setSelected(false);
            followBTN.setText("关注");
        }
    }
}
